<?php
namespace Admin\Controller;
use Think\Controller;
class ReplyController extends Controller {
	
	//自动回复首页
    public function index(){
        $model = M('reply');
        $page = empty(I('p'))?1:I('p');
        //获取数据
        $data = $model->limit(10)->page($page)->select();

        //分页
        $count = $model->count();
        $Page = new \Think\Page($count,10);
        //分页显示输出
        $show = $Page->show();

        //发送到模板并显示
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }

    //添加自动回复
    public function add(){
        if(IS_POST){
            $data = I('post.');
            $reply = D('reply');
            //图片上传
            if($_FILES['file']){
                $upload = new \Think\Upload();
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                $upload->rootPath = './Public/';
                $upload->savePath = 'uploads/';
                $info = $upload->uploadOne($_FILES['file']);
                $data['img'] = $info['savepath'].$info['savename'];
            }
            //验证数据
            if($reply->create($data)){
                $res = $reply->add();
                //判断插入成功
                if($res){
                    $this->success('添加成功',U('Reply/index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($reply->getError());
            }
        }else{
            //关键字
            $keyword = M('menu')->field('key')->where(array('type'=>1))->select();
            $arr = array();
            foreach($keyword as $v){
                $arr[] = $v['key'];
            }

            //发送数据并显示
            $this->assign('keyword',json_encode($arr));
            $this->display();
        }

    }

    //修改自动回复
    public function edit(){
        if(IS_POST){
            $data = I('post.');
            $reply = D('reply');
            //图片上传
            if($_FILES['file']){
                $upload = new \Think\Upload();
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                $upload->rootPath = './Public/';
                $upload->savePath = 'uploads/';
                $info = $upload->uploadOne($_FILES['file']);
                $data['img'] = $info['savepath'].$info['savename'];
            }
            //验证数据
            if($reply->create($data)){
                $res = $reply->save();
                //判断插入成功
                if($res){
                    $this->success('修改成功',U('Reply/index'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error($reply->getError());
            }
        }else{
            $id = I('id');
            //关键字
            $keyword = M('menu')->field('key')->where(array('type'=>1))->select();
            $arr = array();
            foreach($keyword as $v){
                $arr[] = $v['key'];
            }
            //原始数据
            $data = M('reply')->find($id);

            //发送数据并显示
            $this->assign('keyword',json_encode($arr));
            $this->assign('data',$data);
            $this->display();
        }
    }

    //删除自动回复
    public function del(){
        //删除数据
        $res = M('reply')->delete(I('id'));
        //判断对错
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}